package org.lmind.example.uc.biz;

import org.lmind.example.common.core.Attributes;
import org.lmind.example.common.core.HashMapAttributes;
import org.lmind.example.common.entity.User;
import org.lmind.example.uc.api.UserCommonAttrubtes;
import org.lmind.example.uc.api.UserInfo;

public class UserInfoImpl implements UserInfo {
	
	private User user;
	
	private Attributes attributes;

	public UserInfoImpl(User user) {
		this.user = user;
		attributes = readAttributes(user);
	}

	@Override
	public String getUserName() {
		return user.getName();
	}

	@Override
	public boolean validatePassword(String password) {
		return password.equals(this.user.getPassword());
	}

	@Override
	public Attributes getAttributes() {
		return attributes;
	}

	private Attributes readAttributes(User user) {
		HashMapAttributes attributes = new HashMapAttributes();
		if (user.getSex() != null) {
			attributes.setIgnoreDirty(UserCommonAttrubtes.SEX, user.getSex());
		}
		if (user.getBirthday() != null) {
			attributes.setIgnoreDirty(UserCommonAttrubtes.BIRTHDAY, user.getBirthday());
		}
		if (user.getDescription() != null) {
			attributes.setIgnoreDirty(UserCommonAttrubtes.DESCRIPTION, user.getDescription());
		}
		return attributes;
	}
}
